package com.zzty.txj1.controller;



import com.zzty.txj1.entity.*;
import com.zzty.txj1.service.LookTeamExamService;
import com.zzty.txj1.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/lookTeamExam")
public class LookTeamExamController {
    public static List<String>names=new ArrayList<>();
    public static List<String>fileNames=new ArrayList<>();
    @Autowired
    LookTeamExamService lookTeamExamService;
    @Autowired
    UserService userService;
    @RequestMapping("/commentHomework")
    public List<Groups>getGroups(TeamExam teamExam, HttpServletRequest request){
        List<Groups>groups1=new ArrayList<>();
        User user=(User)request.getSession().getAttribute("user");
        teamExam.setClassId(user.getClazzid());
        System.out.println("teamExam"+teamExam.toString());
        Integer id=lookTeamExamService.getId(teamExam);
        System.out.println("id"+id);
        List<Integer>getGroupId=lookTeamExamService.getGroupId(id);
        System.out.println("teamExam"+getGroupId);
        for(int i=0;i<getGroupId.size();i++){
            Group group=lookTeamExamService.getGroupName(getGroupId.get(i));
            System.out.println("getGroupId.get(i)"+getGroupId.get(i));
            System.out.println("getGroupId.get(i)"+group.toString());
            String userName1=group.getUserName1();
            String userName2=group.getUserName2();
            TeamExamTransaction teamExamTransaction=new TeamExamTransaction();
            teamExamTransaction.setGroupId(getGroupId.get(i));
            teamExamTransaction.setTeamExamId(id);
            TeamExamTransaction teamExamTransaction1=lookTeamExamService.getinfo(teamExamTransaction);
            Groups groups=new Groups();
            String userNames=userName1+"_"+userName2;
            Integer groupId=getGroupId.get(i);
            Double score=teamExamTransaction1.getScore();
            String spendTime=teamExamTransaction1.getSpendTime();
            String comment=teamExamTransaction1.getComment();
            groups.setGroupId(groupId);
            groups.setScore(score);
            groups.setUserNames(userNames);
            groups.setSpendTime(spendTime);
            groups.setComment(comment);
            groups1.add(groups);
        }
        return groups1;
    }


    @RequestMapping("/searchHomework")
    public Comment getFileName(Integer groupId, String date,HttpServletRequest request){
        System.out.println("groupID"+groupId+"date"+date);
        TeamExam teamExam=new TeamExam();
        teamExam.setDate(date);
        User user=(User)request.getSession().getAttribute("user");
        teamExam.setClassId(user.getClazzid());
        Integer id=lookTeamExamService.getId(teamExam);
        TeamExamTransaction teamExamTransaction=new TeamExamTransaction();
        teamExamTransaction.setTeamExamId(id);
        teamExamTransaction.setGroupId(groupId);
        TeamExamTransaction teamExamTransaction1=new TeamExamTransaction();
        teamExamTransaction1=lookTeamExamService.getinfo(teamExamTransaction);
        String answerUrl=teamExamTransaction1.getAnswerUrl();
        String comment1=teamExamTransaction1.getComment();
        Double score=teamExamTransaction1.getScore();
        File file = new File(answerUrl);
        names.clear();
        List<String>name=finDiGui(file);
        System.out.println("answer1111"+names);
        Comment comment=new Comment();
        comment.setNames(names);
        comment.setComment(comment1);
        comment.setScore(score);
        System.out.println("getnames"+comment.getNames());
        System.out.println("getnames"+comment.getComment());
        return comment;
    }

    @RequestMapping(value = "/fileNames" )
    public  Map<String,StringBuffer> getFileNames( String name){
        String str="";
        StringBuffer sb=new StringBuffer();
        String result="";
        Map<String,StringBuffer>map=new HashMap<>();
        System.out.println("name"+name);
        System.out.println("fileNames"+fileNames);
        for(int i=0;i<names.size();i++){
            if(name.equals(names.get(i))){
                result=fileNames.get(i);
            }
        }
        try {
            BufferedReader in = new BufferedReader(new FileReader(result));
            while ((str = in.readLine()) != null) {
                sb.append(str);
                sb.append("\n");
        }
        } catch (IOException e) {
        }
        map.put("result",sb);
        return map;
    }

    @RequestMapping(value = "/uploadResult" ,method = RequestMethod.POST)
    public  Integer getFileNames(String date,String comment,Double score,Integer groupId,HttpServletRequest request){
        System.out.println("date"+date+"comment"+comment+"score"+score+"groupID"+groupId);
        TeamExam teamExam=new TeamExam();
        User user=(User)request.getSession().getAttribute("user");
        teamExam.setClassId(user.getClazzid());
        teamExam.setDate(date);
        Integer teamExamId=lookTeamExamService.getId(teamExam);
        TeamExamTransaction teamExamTransaction=new TeamExamTransaction();
        teamExamTransaction.setGroupId(groupId);
        teamExamTransaction.setTeamExamId(teamExamId);
        teamExamTransaction.setScore(score);
        teamExamTransaction.setComment(comment);
        Integer count=lookTeamExamService.updateTeamExamTransaction(teamExamTransaction);
        return count;
    }


    @RequestMapping(value = "/setWinnerId" ,method = RequestMethod.POST)
    public  Integer setWinnerId(Integer winnerId,String date,HttpServletRequest request){
        Group group=new Group();
        TeamExam teamExam=new TeamExam();
        User user=(User)request.getSession().getAttribute("user");
        teamExam.setClassId(user.getClazzid());
        teamExam.setDate(date);
        Integer teamExamId=lookTeamExamService.getId(teamExam);
        group=lookTeamExamService.getGroupName(winnerId);
        String userName1=group.getUserName1();
        String userName2=group.getUserName2();
        Integer id1=userService.getUserByName(userName1).getId();
        Integer id2=userService.getUserByName(userName2).getId();
        TeamExam teamExam1=new TeamExam();
        teamExam1.setId(teamExamId);
        teamExam1.setWinnerId1(id1);
        teamExam1.setWinnerId2(id2);
        Integer count=lookTeamExamService.updateTeamExam(teamExam1);
        return count;
    }



    public List<String>  finDiGui(File file) {
        // 判断是否为文件夹
        if (file.isDirectory()) {
            File[] listFiles = file.listFiles();
            // 执行操作
            for (File f : listFiles) {
                finDiGui(f);
            }
        }
        if (file.getName().endsWith(".txt")||file.getName().endsWith(".java")||file.getName().endsWith(".xml")) {
            fileNames.add(file.getAbsolutePath());
            String[] fileName=file.getName().split("\\\\");
            for(String name:fileName){
                names.add(name);
            }
//            System.out.println(file);
        }
        return names;
    }
}
